<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>

<table id="mtService_table" class="table table-bordered">
</table>
<script type="text/javascript">
    $(function () {
        var table_cl = $('#mtService_table').datagrid({
            height: '100%',
            width:'100%',
            fullGrid:true,
            gridTitle: '微服务',
            showToolbar: false,
            filterThead: false,
            delPK: 'id',
            dataUrl: '/admin/micro/service/services',
            local: "local",
            inlineEditMult: false,
            saveAll: false,
            addLocation: 'last',
            delConfirm:true,
            columns: [
                {
                    render: loadKey,
                    label: '标识'
                }, {
                    name: 'url',
                    label: '请求地址',
                    align: 'center',
                    rule:  'required'
                }, {
                    render:mtServiceStatusRender,
                    label: '状态',
                    align: 'center'
                }
            ],
            paging: {pageSize: 20, pageIndex: 1},
            linenumberAll: true
        });

    });

    function loadKey(value, data){
        return '<a href="javascript:;" onclick="openNewTable(\''+data.key+'\')">'+data.key+'</a>';
    }

    /**
     * 状态列返回函数
     */
    function mtServiceStatusRender(value, data) {
        if (data.status) {//开状态
            return '<div data-status="false" data-val = "' + data.key + '" class="selectBtn" onclick="updateMtServiceStatus(this);"><div class="openWord">开</div><div class="open1 select_01"><div class="open2 select_02"></div></div><div class="closeWord">关</div></div>';
        } else {
            return '<div data-status="true" data-val = "' + data.key + '" class="selectBtn" onclick="updateMtServiceStatus(this);"><div class="openWord">开</div><div class="open1 select_01 close1"><div class="select_02"></div></div><div class="closeWord">关</div></div>';
        }
    }


    function updateMtServiceStatus(e) {
        var $this = $(e);
        var key = $this.data("val");
        var status = $this.data("status");
        if (key === null || key === "" || typeof (key) === "undefined" || status === null || status === "" || typeof (status) === "undefined") {
            BJUI.alertmsg('error', "参数错误，重新操作", {
                okCall: function () {
                    $('#mtService_table').datagrid("refresh",true);
                }
            });
            return;
        }
        if(key === "config"){
            BJUI.alertmsg('ok', "系统配置服务不可操作");
            return;
        }
        BJUI.ajax('doajax', {
            url: '/admin/micro/service/updateStatus',
            loadingmask: true,
            data: {"key": key, "status": status},
            callback: function (response, options) {
                if (response.haveError) {
                    BJUI.alertmsg('error', response.errMsg, {
                        okCall: function () {
                            $('#mtService_table').datagrid("refresh",true);
                        }
                    });

                } else {
                    BJUI.alertmsg('ok', response.data, {
                        okCall: function () {
                            $('#mtService_table').datagrid("refresh",true);
                        }
                    });
                }
            }
        });
    };

    function openNewTable(key){
        BJUI.navtab({
            id:key+'Service',
            url:'/admin/micro/service/getServiceApiPage/'+key,
            title:key+'微服务'
        });

//        BJUI.dialog({
//            id: 'save_payer',
//            url: url,
//            title: '编辑',
//            resizable: true,
//            width: 850,
//            height: 400,
//            fresh:true
//        });
    }
</script>